(function () {
    /*Game类*/
    var Game = window.Game = function(param) {
        //获取画布
        this.myCanvas = document.querySelector(param.canvasid);
        //获取上下文
        this.ctx = this.myCanvas.getContext("2d");

        this.timer = null;
        this.fno = 0;
        //设置分数

        var that = this;
        this.init();
        this.loadAllResource(function () {
            that.start();
            // that.bindEvent();
        });
        //判断是否移动端
        this.isMobile = false;
    };
    Game.prototype.init = function () {
        var windowW = document.documentElement.clientWidth;
        var windowH = document.documentElement.clientHeight;
        if(windowW > 414){
            windowW = 414;
        }else if(windowW < 320){
            windowW = 320;
        }
        //812
        if(windowH > 812){
            windowH = 812;
        }else if(windowH < 500){
            windowH = 500;
        }
        this.ctx.canvas.width = windowW;
        this.ctx.canvas.height = windowH;

    };
    //读取资源
    Game.prototype.loadAllResource = function (callback) {
        var that = this;
        this.imgList = {};
        var aleadyLoadNum = 0;
        //创建异步对象
        var ajaxObj = new XMLHttpRequest();
        ajaxObj.open("get","image.json",true);
        ajaxObj.send();
        ajaxObj.onreadystatechange = function () {
            if(ajaxObj.readyState==4 && ajaxObj.status==200){
                //获取数据，转换数据成对象
                var reponseArr = JSON.parse(ajaxObj.responseText);
                for(var i=0; i<reponseArr.length; i++){
                    that.imgList[reponseArr[i].name] = reponseArr[i].name;
                    //创建image对象
                    that.imgList[reponseArr[i].name] = new Image();

                    //加载图片
                    that.imgList[reponseArr[i].name].onload = function () {
                        aleadyLoadNum++;
                        //绘制加载数据
                        var txt = "小鸟正在路上"+aleadyLoadNum+"/"+reponseArr.length+"，马上就飞到了~";
                        that.ctx.font = "16px Microsoft Himalaya consolas";
                        that.ctx.clearRect(0,0,that.ctx.canvas.width,that.ctx.canvas.height);
                        that.ctx.fillText(txt,that.ctx.canvas.width/2,that.ctx.canvas.height*(1-0.618));
                        that.ctx.textAlign = "center";
                        //判断是否加载完成
                        if(aleadyLoadNum==reponseArr.length){
                            callback();
                        }
                    };
                    that.imgList[reponseArr[i].name].src = reponseArr[i].url;

                }
            }
        }
    };
    //开始游戏
    Game.prototype.start = function () {
        if(document.documentElement.clientWidth == this.ctx.canvas.width){
            this.isMobile = true;
        }
        //实例化场景
        this.scene = new SceneManager();
        //创建数字图片数组
        this.numList = new Array();
        for(var i=0;i<=9;i++){
            this.numList.push(this.imgList["num"+i]);
        }

        var that = this;
        clearInterval(this.timer);
        this.timer = setInterval(function () {
            that.ctx.clearRect(0,0,that.ctx.canvas.width,that.ctx.canvas.height);
            //渲染场景
            that.scene.render();
            that.scene.update();


            that.ctx.font = "16px consolas";
            that.ctx.fillStyle = "black";
            that.ctx.textAlign = "left";
            that.ctx.fillText("fno:"+that.scene.fno,10,20);
            that.ctx.fillText("场景:"+that.scene.sceneNum,10,40);

            // that.ctx.beginPath();
            // that.ctx.moveTo(that.ctx.canvas.width/2,0);
            // that.ctx.lineTo(that.ctx.canvas.width/2,500);
            // that.ctx.stroke();

        },20);

        this.scene.bindEvent();
    };
}());

